<template>
  <view class="page-container">
    <!-- 顶部标签栏 -->
    <u-sticky bgColor="#ffffff">
      <view class="header">
        <u-tabs 
          :current="index" 
          :list="tabs" 
          :scrollable="false"
          active-color="#2979ff"
          inactive-color="#606266"
          bar-width="80"
          bar-height="4"
          height="88"
          font-size="34"
		  @change="tabChange"
        ></u-tabs>
      </view>
    </u-sticky>
    
    <!-- 主要内容区域 -->
    <view class="content">
		<!-- 岗位列表 -->
		<view v-show="index == 0">
			<!-- 热门岗位标题 -->
			<view class="section-title">
			  <image src="../../static/hot.png" class="title-icon"></image>
			  <text class="title-text">热门岗位</text>
			</view>
			
			<!-- 岗位网格 -->
			<view class="position-grid">
			  <view 
			    class="position-card" 
			    v-for="(position, idx) in positions" 
			    :key="position.id"
			    :class="{'position-card-active': activeIndex === idx}"
			    @click="handleClickPostion(position.id)"
			    @touchstart="activeIndex = idx"
			    @touchend="activeIndex = -1"
			  >
			    <view class="position-name-wrap">
			      <text class="position-name">{{position.name}}</text>
			    </view>
			    <text class="position-desc">点击开始模拟面试</text>
			  </view>
			</view>
		</view>
		
		<!-- 报告 -->
		<view v-show="index == 1">
			<view class="report-table">
				<view class="table-header">
					<view class="header-cell position">岗位名称</view>
					<view class="header-cell score">综合得分</view>
				</view>
				<view v-for="(report, index) in reports" :key="report.reportId" class="table-row" @click="handleReportClick(report.reportId)">
					<view class="cell index">{{ index + 1 }}</view>
					<view class="cell position">{{ report.positionName }}</view>
					<view class="cell scorePass" v-if="report.overallScore >= 60">{{ report.overallScore }}</view>
					<view class="cell scoreFail" v-else>{{ report.overallScore }}</view>
				</view>
			</view>
		</view>
    </view>
  </view>
</template>

<script>
  import { listAll, genQuestion } from '@/api/interview/position';
  import { listReports,reportDetail } from "@/api/interview/report.js"
  
  export default {
    data() {
      return {
        index: 0,
        activeIndex: -1,
        tabs: [{
          name: '模拟面试',
        }, {
          name: '面试报告',
        }],
        positions: [],
		reports: []
      };
    },
    methods: {
		tabChange(item){
			this.index = item.index
			if(this.index == 0){
				// 重新加载岗位信息
				this.getPostions()
			}else{
				// 重新加载报告信息
				this.getReports()
			}
		},
		getPostions() {
		  listAll().then(res => {
			this.positions = res.data
		  })
		},
		getReports() {
		  listReports().then(res => {
			this.reports = res.data
			console.log('报告列表', this.reports)
		  })
		},
        handleClickPostion(positionId) {
          uni.showLoading({
            title: '准备面试中...'
        });
        genQuestion(positionId).then(res => {
          uni.setStorageSync("interviewRecord", res.data);
          uni.hideLoading();
          uni.navigateTo({
            url: '/pages/answer/answer'
          })
        }).catch(() => {
          uni.hideLoading();
        });
      },
      handleReportClick(reportId) {
        uni.navigateTo({
          url: `/pages/report/report?id=${reportId}`
        });
      }
    },
    onLoad() {
      this.getPostions();
    }
  }
</script>

<style lang="scss" scoped>
.page-container {
  min-height: 100vh;
  background-color: #f7f8fa;
  display: flex;
  flex-direction: column;
}

.header {
  background-color: #ffffff;
  box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.05);
}

.content {
  flex: 1;
  padding: 30rpx;
  padding-bottom: 60rpx;
}

.section-title {
  display: flex;
  align-items: center;
  margin-bottom: 40rpx;
  .title-icon {
    width: 50rpx;
    height: 50rpx;
    margin-right: 20rpx;
  }
  .title-text {
    font-size: 36rpx;
    font-weight: bold;
    color: #303133;
  }
}

.position-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 20rpx;
}

.position-card {
  background-color: #ffffff;
  border-radius: 16rpx;
  padding: 20rpx;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.05);
  transition: all 0.3s;
  aspect-ratio: 1/1;
  min-height: 0;
  &-active {
    transform: scale(0.98);
    box-shadow: 0 2rpx 6rpx rgba(0, 0, 0, 0.1);
    background-color: #f5f7fa;
  }
  .position-name-wrap {
    width: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
    flex: 1;
    text-align: center;
    word-break: break-word;
    padding: 10rpx 0;
    min-height: 0;
  }
  .position-name {
    font-size: 32rpx;
    color: #303133;
    font-weight: 500;
    line-height: 1.4;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
    text-overflow: ellipsis;
  }
  .position-desc {
    font-size: 24rpx;
    color: #909399;
    margin-top: 10rpx;
  }
}

.report-table {
  width: 100%;
  border-collapse: collapse;
  margin-bottom: 20rpx;
}

.table-header {
  display: flex;
  background-color: #f5f7fa;
  font-weight: bold;
}

.table-row {
	height: 100rpx;
	display: flex;
	flex-direction: row;
	align-items: center;
	background-color: #ffffff;
	border-bottom: 1rpx solid #909399;
	border-radius: 16rpx;
	transition: background 0.2s;
	cursor: pointer;
}

.table-row:hover {
  background-color: #e6f7ff;
}

.header-cell,
.cell {
  padding: 20rpx 0;
  font-size: 32rpx;
}

.header-cell.index,
.cell.index {
  width: 60rpx;
  color: #909399;
  text-align: center;
  flex-shrink: 0;
}

.header-cell.position {
	flex: 1;
	text-align: center;
	padding-left: 25rpx;
    font-size: 32rpx;
}
.cell.position {
	flex: 1;
    color: #2979ff;
	text-align: left;
	padding-left: 30rpx;
}

.header-cell.score {
	flex: 1;
	text-align: center;
	padding-right: 25rpx;
    font-size: 32rpx;
}
.cell.scorePass {
  flex: 1;
  color: #2979ff;
  text-align: right;
  padding-right: 170rpx;
}
.cell.scoreFail {
  flex: 1;
  color: red;
  text-align: right;
  padding-right: 170rpx;
}
</style>